const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const cors = require("cors");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const app = express();
const fileRoutes = require("./routes/fileuploadRoute");

dotenv.config({ path: "./config.env" });
const Local_DB = process.env.DATABASE_LOCAL_URL;
const mongoUri =
  "mongodb+srv://oldboy:LGNAWA8I2UAgutbZ@cluster0.ipcxx.mongodb.net/priceDB?retryWrites=true";

mongoose
  .connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log("连接成功！");
  })
  .catch((error) => {
    console.log("Altas Cluster connection error", error);
  });

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 配置静态资源目录 整一个文件夹 通过域名能访问
app.use("/public", express.static(path.join(__dirname, "public")));

app.use("/upload", fileRoutes);

const hostname = process.env.HOSTNAME;
const port = process.env.PORT;

app.listen(port, hostname, () => {
  console.log(`file-server running at http://${hostname}:${port}/`);
});
